<template>
  <div class="main-content">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <!-- :search-func="handleQuery"
        :reset="resetQuery" -->
      <SearchFrame btnName="添加协议" :hasPermi="['logic:agreement:add']" :btnFunc="handleAddOrEdit" :model="queryParams">
        <!-- <SearchFrameItem prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入协议名称" clearable />
        </SearchFrameItem> -->
      </SearchFrame>
    </transition>
    <el-table v-loading="loading" :data="agreementList">
      <el-table-column label="序号" class-name="num" align="left" width="120">
        <template #default="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议类型" align="left" prop="title" show-overflow-tooltip>
        <template #default="scope">
          {{ agreementFilter(scope.row.dtype) }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="left" prop="sort"></el-table-column>
      <el-table-column label="状态" align="left" prop="isActive">
        <template #default="scope">
          <!-- @change="toggleStatus(scope.row)"
          <el-switch v-model="scope.row.isActive"></el-switch> -->
        </template>
      </el-table-column>
      <!-- <el-table-column label="发布时间" align="left" prop="publishTime" show-overflow-tooltip></el-table-column> -->
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" @click="handleAddOrEdit(scope.row.id)" v-hasPermi="['logic:agreement:edit']">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)" v-hasPermi="['logic:agreement:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup lang="ts" name="Agreement">
import { listDist, getDist, delDist, addDist, updateDist } from '@/api/logic/dist';
import { ArticleVO, ArticleQuery, ArticleForm } from '@/api/logic/article/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { logic_language_type, logic_isactive } = toRefs<any>(proxy?.useDict('logic_language_type', 'logic_isactive'));

const route = useRoute();
const router = useRouter();

const agreementList = ref<ArticleVO[]>([]);
const loading = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    categoryId: '',
    title: '',
  },
});

const { queryParams } = toRefs(data);

/** 查询协议列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDist(queryParams.value);
  // res.rows.forEach(el => el.isActive = !!(Number(el.isActive)))
  agreementList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

// 协议类型过滤
const agreementFilter = (val) => {
  const map = {
    'yhxy': '用户协议',
    'yszc': '隐私政策'
  }
  return map[val]
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value.pageSize = 10;
  handleQuery();
}

/** 新增及修改按钮操作 */
const handleAddOrEdit = async (id: string) => {
  router.push({
    path: '/workspace/addOrEditAgreement',
    query: {
      agreementId: id || ''
    }
  })
}

/** 删除按钮操作 */
const handleDelete = async (row) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确认要删除改该协议吗？').finally(() => loading.value = false);
  await delDist(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}
// // 修改状态操作
// const toggleStatus = async (row) => {
//   try {
//     const res = await changeStatus({ id: row.id, isActive: row.isActive ? 0 : 1 })
//     proxy?.$modal.msgSuccess(res.msg);
//     getList()
//   } catch (e) {
//     console.log(e)
//   }
// }
onMounted(() => {
  getList();
})
onActivated(() => {
  getList();
});
</script>
